// pages/getphone/getphone.js
import { Getphone } from 'getphone.model.js';
var getphone = new Getphone();

const telRule = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[1|3|5|8|9])[0-9]{8}$/;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    iscustomer:true,//是否有账号
    getyzm:true,
    countdownnum:60,
    inputtel:'',
    inputyzm:''
  },
 
  // 登录注册切换
  // iscustomer(){
  //   if(this.data.iscustomer){
  //     this.setData({
  //       iscustomer:false,
  //       inputValue:'',
  //       getyzm:true,
  //     })
  //   }else{
  //     this.setData({
  //       iscustomer:true,
  //       inputValue:'',
  //       getyzm:true,
  //     })
  //   }
    
  // },

  // 获取验证码
  yzm(){
    console.log('获取验证码')
    const reg=this.regPhone()
    if(reg){
      getphone.ttTelCode(this.data.inputtel,(res)=>{
        if(res.code){
          tt.showToast({
            title: "发送成功",
            duration: 2000,
          });
        }
        this.setData({
          getyzm:false
        })
        this.countdown()
      })
    }
  },
// 倒计时
  countdown(){
    let that =this
    const time = setInterval(()=>{
      that.setData({
        countdownnum:that.data.countdownnum-=1
      })
      if(that.data.countdownnum == 0){
        clearInterval(time)
        that.setData({
          getyzm:true,
          countdownnum:60
        })
      }
    },1000)
  },
  // 监听input输入
  handleInput(e){
    console.log(e.detail.value)
    this.setData({
      inputtel:e.detail.value
    })
  },
  handleyzm(e){
    this.setData({
      inputyzm:e.detail.value
    })
  },
  // 登录注册
  login(){
    const reg=this.regPhone()
    const yzm=this.data.inputyzm
    const tel=this.data.inputtel
    let code = tt.getStorageSync('code');
    let anonymousCode = tt.getStorageSync('anonymousCode');
    console.log('点击登录',code)
    if(code == ''){
      tt.login({
        force: true,
        success(res) {
          console.log(`login 调用成功`,res);
          tt.setStorageSync('code', res.code);
          code=res.code
          tt.setStorageSync('anonymousCode', res.anonymousCode);
          anonymousCode=res.anonymousCode
        },
        fail(res) {
          console.log(`login 调用失败`);
        },
      });
    }
    if(reg && yzm != ''){
      tt.showLoading({
        title: '加载中', // 内容
      });
      getphone.ttTelLogin(tel,yzm,code,anonymousCode,(res)=>{
        console.log('登录',res)
        tt.hideLoading();
        if(res.code == 400){
          tt.showToast({
            title: res.message,
            duration: 2000,
            icon:fail
          });
          return false
        }else if(res.code == 201){
          tt.showToast({
            title: '登录成功',
            duration: 2000,
            success(){
              tt.setStorageSync('token', res.data.token);
              console.log(tt.getStorageSync('returnurl') == '')
              if(tt.getStorageSync('returnurl') == ''){
                console.log('跳转首页')
                tt.switchTab({
                  url: '/pages/index/index' // 指定页面的url
                });
              }else{
                tt.navigateTo({
                  url: tt.getStorageSync('returnurl') // 指定页面的url
                });
              }
              tt.removeStorageSync('returnurl');
            }
          });
        }else{
          tt.showToast({
            title: '系统错误，稍后再试',
            duration: 2000,
            icon:'fail'
          });
          this.onLoad()
        }
      })
    }else{
      tt.showToast({
        title: '验证码不能为空',
        duration: 2000,
      })
    }
    setTimeout(()=>{
      tt.hideLoading();
    },3000)
  },

  // 注册
  // reg(){
  //   const reg=this.regPhone()
  // },

  // 验证手机号码
  regPhone(){
    const phone=this.data.inputtel
    if(phone == ''){
      tt.showToast({
        title: "请输入手机号码",
        duration: 2000,
        icon:'fail'
      });
      return false
    }else if(!telRule.test(phone)){
      tt.showToast({
        title: "手机号码格式错误",
        duration: 2000,
        icon:'fail'
      });
      return false
    }else{
      return true
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  }
});